var _FragmentConstance = require('scenes/game/fragments/constance.js');
var _Constance = require('scenes/game/constant.js');
var _Tip = require('./labels.js');
var _Step = require('./step.js');
var _vec3f = require('core/glm.js').vec3f;

var S = require('res/style.js');

function createTip(text) {
    var text = S.ui.tip.createText(text);
    text.setAnchor(0.5, 0.5);
    return text;
}

// ==========================
// Toturials
// ==========================
function TextFragment(pane, size) {
    this.mPane = pane;
    this.mLastId = '';
    this.mViewCount = 0;
    size.scale(0.5, this.mCenter = new _vec3f());
}
TextFragment.prototype.findView = function (id) {
    if (!id || id.length == 0) {
        return null;
    }
    return this.mPane.getElementById(id);
};
TextFragment.prototype.hide = function () {
    var preview = this.findView(this.mLastId);
    if (preview) {
        this.mLastId = '';
        preview.fadeOut();
    }
};
TextFragment.prototype.showText = function (text) {
    var preview = this.findView(this.mLastId);
    if (preview) {
        preview.fadeOut();
    }

    if (typeof text == 'string') {
        text = createTip(text)
    }
    var view = new _Tip(text);
    view.setPosition(this.mCenter);
    this.mPane.addFragment(view);
    view.fadeIn();
    view.setId(this.mLastId = this.createId());

    view.getEvent().on('fadeout', this.textInvisiable, this);
    return view;
};
TextFragment.prototype.textInvisiable = function (id) {
    var frag = this.mPane.findFragment(id);
    if (frag) {
        frag.remove();
    }
};
TextFragment.prototype.createId = function () {
    return 'text_' + (this.mViewCount++);
};

// ==========================
// Toturials
// ==========================
function Toturials(gamearea) {
    this.mSteps = [
        './steps/s01_swip_any_where_to_begin.js',
        './steps/s02_meet_1_and_2.js',
        './steps/s03_swipe_anywhere_move_anybody.js',
        './steps/s04_use_the_walls_to_add_1_and_2.js',
        './steps/s05_auspicious.js',
        './steps/s06_new_numbers_appear.js',
        './steps/s07_3_3_eq_6.js',
        './steps/s08_you_are_getting_it.js',
        './steps/s09_numbers_3_and_higher_will_twins.js',
        './steps/s10_create_a_12_to_continue.js',
        './steps/s11_fantastic.js',
        './steps/s12_1_will_only_add_with_2.js',
        './steps/s13_make_a_24.js',
        './steps/s14_this_is_mr3.js',
        './steps/s15_make_highest_number_as_you_can.js',
        './steps/s16_look_up_to_see_what_comes_next.js',
        './steps/s17_its_over_when_the_board_fills_up.js'
    ];

    this.mGamearea = gamearea;
    this.mTouchDelegate = module.$('../swiptrigger.js');

    this.mLocal = _Constance.getLocal();
    this.mLocal.add('toturial', this);
    this.mLocal.add('toturial.swip', this.mTouchDelegate);

    this.mLocal.find(_Constance.RELEASE_ID).push(module);
    this.mLocal.find(_Constance.RELEASE_ID).push(require('native_module').getCached(module.absoultePath('./step.js')));
    this.mLocal.find(_Constance.RELEASE_ID).push(require('native_module').getCached(module.absoultePath('./labels.js')));

    this.mHeader = new TextFragment(this.mLocal.find('ui.header'), _FragmentConstance.headerSize);
    this.mFooter = new TextFragment(this.mLocal.find('ui.footer'), _FragmentConstance.footerSize);

    this.mCurrent = this.mEmpty = new _Step();// empty toturial to block undefined error
}
Toturials.prototype.getTouchDelegate = function () {
    return this.mTouchDelegate;
};

Toturials.prototype.onMergeBefore = function (cells) {
    this.mCurrent.onMergeBefore(cells);
};
Toturials.prototype.onMergeFinish = function (cells) {
    this.mCurrent.onMergeFinish(cells);
};
Toturials.prototype.onAbsorbStart = function (cancel) {
    this.mCurrent.onAbsorbStart(cancel);
};
Toturials.prototype.onSlideinStart = function (cell) {
    this.mCurrent.onSlideinStart(cell);
};
Toturials.prototype.onSlideinFinish = function () {
    this.mCurrent.onSlideinFinish();
};

// ---------------- interact with view
Toturials.prototype.hideHeader = function () {
    this.mHeader.hide();
};
Toturials.prototype.hideFooter = function () {
    this.mFooter.hide();
};
/**
 * show tip to footer of screen
 * @param text
 */
Toturials.prototype.showHeaderTip = function (text) {
    return this.mHeader.showText(text);
};
Toturials.prototype.showFooterTip = function (text) {
    return this.mFooter.showText(text);
};

Toturials.prototype.nextStep = function () {
    if (this.mSteps.length == 0) {
        this.mCurrent = this.mEmpty;
        this.mGamearea.toturialFinished();
    } else {
        var path = this.mSteps.shift();
        var stepmodule = require(path);
        this.mLocal.find(_Constance.RELEASE_ID).push(require('native_module').getCached(module.absoultePath(path)));

        this.mCurrent = new stepmodule();
        this.mCurrent.onStart(this, this.mLocal);
    }
};

module.exports = Toturials;